/**
 * Created by Administrator on 2016/11/22.
 */
var mongo = require('./db');

function Comment(name, second, title, comment) {
    //留言的用户
    this.name = name;
    //留言的时间
    this.second = second;
    //留言的标题
    this.title = title;
    //以上三个是查询条件
    //才是留言的内容
    this.comment = comment
}
//保存留言的方法
Comment.prototype.save = function (callback) {
    var name = this.name;
    var second = this.second;
    var title = this.title;
    var comment = this.comment;
    //搜集一下信息
    mongo.open(function (err, db) {
        if (err) {
            return callback(err)
        }
        db.collection('posts', function (err, collection) {
            if (err) {
                mongo.close();
                return callback(err)
            }
            //保存留言到对应的文章的comments里面
            collection.update({
                "name": name,
                "time.second": second,
                "title": title
            }, {
                $push: ({"comments": comment})
            }, function (err) {
                mongo.close();
                if (err) {
                    return callback(err)
                }
                callback(null)
            })

        })
    })


}

module.exports = Comment;